package cn.com.cis.web.actions;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import cn.com.cis.domain.DatabaseType;
import cn.com.cis.service.DataBaseService;
import cn.com.cis.utils.FileUtils;
import cn.com.cis.web.bean.DatabaseTypeActionBean;

@Controller
@RequestMapping(value = "/databasetype")
public class DatabaseTypeController {

    @Autowired
    private DataBaseService databaseService;
    
    private void setNavigateBar(ModelAndView mv){
    	mv.addObject("icon", "icon-list");
        mv.addObject("title", "驱动管理");
    }

    @RequiresPermissions("配置管理:驱动管理:查看驱动列表")
    @RequestMapping(value = "/list")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("databasetype/list");
        setNavigateBar(mav);
        
        mav.addObject("jarFiles", FileUtils.getDriverJars(request));
        
        return mav;
    }

    @RequiresPermissions("配置管理:驱动管理:添加驱动")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("databasetype/add");
        setNavigateBar(mav);
        
        mav.addObject("jarFiles", FileUtils.getDriverJars(request));
        
        return mav;
    }

    @RequiresPermissions("配置管理:驱动管理:添加驱动")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView save(DatabaseTypeActionBean bean, RedirectAttributesModelMap modelMap) {
        ModelAndView mav = new ModelAndView("redirect:/databasetype/list");
        setNavigateBar(mav);
        
        DatabaseType d = databaseService.selectDatabaseTypeById(bean.getType());
        if (d != null) {
            mav.setViewName("redirect:/databasetype/add");
            modelMap.addFlashAttribute("error", "已存在名称为" + bean.getType() + "的数据源类型.");
            return mav;
        }
        
        DatabaseType databaseType = new DatabaseType();
        databaseType.setType(bean.getType());
        databaseType.setDescription(bean.getDescription());
        databaseType.setUrl(bean.getUrl());
        databaseType.setDriver(bean.getDriver());
        
        databaseService.insertDatabaseType(databaseType);
        
        return mav;
    }

    @RequiresPermissions("配置管理:驱动管理:编辑驱动")
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String type) {
        ModelAndView mav = new ModelAndView("databasetype/edit");
        setNavigateBar(mav);

        DatabaseType databasetype = databaseService.selectDatabaseTypeById(type);

        mav.addObject("databasetype", databasetype);
        
        return mav;
    }

    @RequiresPermissions("配置管理:驱动管理:编辑驱动")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ModelAndView update(DatabaseTypeActionBean bean, RedirectAttributesModelMap modelMap) {
        ModelAndView mav = new ModelAndView("redirect:/databasetype/list");
        setNavigateBar(mav);
        
        DatabaseType databaseType = new DatabaseType();
        databaseType.setType(bean.getType());
        databaseType.setDescription(bean.getDescription());
        databaseType.setUrl(bean.getUrl());
        databaseType.setDriver(bean.getDriver());

        databaseService.updateDatabaseType(databaseType);
        
        return mav;
    }

    @RequiresPermissions("配置管理:驱动管理:删除驱动")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public ModelAndView del(String type, RedirectAttributesModelMap modelMap) {
        ModelAndView mav = new ModelAndView("redirect:/databasetype/list");
        setNavigateBar(mav);
        
        int c = databaseService.selectDatabaseByType(type);
        if (c > 0) {
            modelMap.addFlashAttribute("error", "数据源类型已被引用，不能删除。");
        } else {
            databaseService.deleteDatabaseType(type);
        }
        
        return mav;
    }

}
